home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / GRAPHICS / KS_SP.ZIP / KS_SP.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1995-09-18  |  2.1 KB  |  119 lines

  1. //ks_sp.cpp
  2.  
  3. //texture mapping onto a sphere by Keith/Pixel Magic
  4.  
  5. //.raw format is first 2 words width/height then byte data.
  6.  
  7. //Comments/suggestions welcome : ks2@st-and.ac.uk
  8.  
  9. //Also uploaded to x2ftp with binaries and raws
  10.  
  11. //If you use it, please credit me
  12.  
  13. #include <conio.h>
  14. #include <math.h>
  15. #include <dos.h>
  16. #include <stdio.h>
  17.  
  18. #pragma inline
  19.  
  20. int R;        //Radius of sphere
  21. int D;        //Diameter of sphere
  22. int C;        //Circum of sphere
  23. int* Rh;    //Rad at height
  24.  
  25. char far* bm;
  26. int W;        //Width of bm
  27. int H;        //Height of bm
  28.  
  29. int** TRF;    //Horizontal transformations
  30. int* TRFV;    //Vertical (scan line) transformations
  31.  
  32. //Externals which I have not included
  33. struct pal;
  34. extern char far* LoadRAW(char*,int&,int&);
  35. extern pal*     LoadPal(char*);
  36. extern void*     SetPal(pal* p);
  37.  
  38. main(int argc,char** argv)
  39. {
  40.  char buf[15];
  41.  
  42.  if(argc!=2) return(0);
  43.  
  44.  sprintf(buf,"%s.raw",argv[1]);
  45.  
  46.  bm=LoadRAW(buf,W,H);
  47.  
  48.  sprintf(buf,"%s.pal",argv[1]);
  49.  
  50.  pal* p=LoadPal(buf);
  51.  
  52.  if(bm==NULL || p==NULL) return(0);
  53.  
  54.  C=W;
  55.  D=(float)C/M_PI;
  56.  R=(float)(C)/(M_PI*2.0);
  57.  
  58.  register int i,j;
  59.  
  60.  Rh=new int[D];
  61.  for(i=0;i<D;i++) Rh[i]=sqrt(R*R-(R-i)*(R-i));
  62.  
  63.  TRF=new int*[D];
  64.  for(i=0;i<D;i++) TRF[i]=new int[Rh[i]];
  65.  
  66.  for(i=0;i<D;i++)
  67.     for(j=0;j<Rh[i];j++) {
  68.         double theta=asin((float)j/(float)Rh[i]);
  69.         float arc=(float)C/4.0;
  70.         float fractional=theta/(M_PI/2.0);
  71.         int offset=fractional*arc;
  72.         TRF[i][j]=offset; }
  73.  
  74.  TRFV=new int[D];
  75.  
  76.  for(i=0;i<D;i++) {
  77.     float dh=(float)H/(float)D;
  78.     int line=(float)i*dh;
  79.     TRFV[i]=W*line; }
  80.  
  81.  int x=160;
  82.  
  83.  _AH=0;        //Set 13h
  84.  _AL=0x13;
  85.  asm int 0x10
  86.  
  87.  SetPal(p);
  88.  
  89.  char far* vga=(char far*)MK_FP(0xA000,0);
  90.  
  91.  int rot=0;
  92.  
  93.  char k=0;
  94.  
  95.  while(k!=27) {
  96.     if(kbhit()) k=getch();
  97.     rot++;
  98.     rot%=W;
  99.     for(i=0;i<D;i++) {
  100.         char far* loc=bm+TRFV[i]+rot;
  101.         int i320=i*320;
  102.         for(j=0;j<Rh[i];j++) {
  103.             int h_offset=TRF[i][j];
  104.             char c1=*(loc+h_offset);
  105.             char c2=*(loc-h_offset);
  106.             vga[x+j+i320]=c1;
  107.             vga[x-j+i320]=c2; }
  108.         }
  109.     }
  110.  
  111.  _AH=0;
  112.  _AL=0x03;
  113.  asm int 0x10
  114.  
  115.  printf("Texture map: Keith\\Pixel Magic\n");
  116.  
  117.  return(0);
  118. }
  119.